package cn.wzut.edut.dao.inf;

import cn.wzut.edut.dao.SnowflakeIdWorker;
import cn.wzut.edut.dao.mapper.PtyClassMapper;
import cn.wzut.edut.dao.mapper.PtyCollegeMapper;
import cn.wzut.edut.dao.mapper.PtyStuMapper;
import cn.wzut.edut.dao.mapper.PtyTeacherMapper;
import cn.wzut.edut.dao.pojo.inf.StuInf;
import cn.wzut.edut.dao.pojo.inf.TcInf;
import cn.wzut.edut.dao.pojo.table.*;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 信息管理
 */
@Repository
public class Information {
    @Autowired
    private PtyStuMapper ptyStuMapper;
    @Autowired
    private PtyTeacherMapper ptyTeacherMapper;
    @Autowired
    private PtyClassMapper ptyClassMapper;
    @Autowired
    private PtyCollegeMapper ptyCollegeMapper;
    /**
     * 根据id查询学生信息
     */
    public PtyStu selectStuInId(String id){
        QueryWrapper<PtyStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",id);
        return ptyStuMapper.selectOne(queryWrapper);
    }
    /**
     * 根据学院id查询学生信息
     */
    public List<PtyStu> selectStuInCollege(String college_id){
        QueryWrapper<PtyStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("college_id",college_id);
        return ptyStuMapper.selectList(queryWrapper);
    }
    /**
     * 根据班级id查询学生信息
     */
    public List<PtyStu> selectStuInClass(String class_id){
        QueryWrapper<PtyStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("class_id",class_id);
        return ptyStuMapper.selectList(queryWrapper);
    }
    /**
     * 根据id查询教师信息
     */
    public PtyTeacher selectTeacherInId(String id){
        QueryWrapper<PtyTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",id);
        return ptyTeacherMapper.selectOne(queryWrapper);
    }
    /**
     * 根据学院id查询教师信息
     */
    public List<PtyTeacher> selectTeacherInCollege(String college_id){
        QueryWrapper<PtyTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("college_id",college_id);
        return ptyTeacherMapper.selectList(queryWrapper);
    }

    /**
     * 查询学生基本信息
     * 根据user_id查询学生表
     * @return StuInf
     */
    public PtyStu selectStu(String user_id){
        /**
         * q1：按照user_id,查询学生信息
         * q2：按照class_id，查询班级名称
         * q3：按照college_id，查询二级学院名称
         */
        StuInf stuInf = new StuInf();
        QueryWrapper<PtyStu> q1 = new QueryWrapper<>();
        q1.in("user_id",user_id);

        return ptyStuMapper.selectOne(q1);

    }

    /**
     * 查询学生基本信息
     * 根据id查询学生表
     * @return StuInf
     */
    public StuInf selectPtyStuInId(String id){
        /**
         * q1：按照user_id,查询学生信息
         * q2：按照class_id，查询班级名称
         * q3：按照college_id，查询二级学院名称
         */
        StuInf stuInf = new StuInf();
        QueryWrapper<PtyStu> q1 = new QueryWrapper<>();
        q1.in("id",id);

        PtyStu ptyStu = ptyStuMapper.selectOne(q1);


        try {

            /**
             * 将对应的数据放入stuInf中输出
             */
//            String s = ptyClassMapper.selectOne(q2).getClassName();
//            stuInf.setClass_name(s);
//            stuInf.setCollege_name(ptyCollegeMapper.selectOne(q3).getCollegeName());
            stuInf.setClass_name("1");
            stuInf.setCollege_name("1");
            stuInf.setId(ptyStu.getId());
            stuInf.setSex(ptyStu.getSex());
            stuInf.setOpen_id(ptyStu.getOpenId());
            stuInf.setPhone(ptyStu.getPhone());
            stuInf.setUser_id(ptyStu.getUserId());
            return stuInf;
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 查询教师基本信息
     * 根据user_id查询教师表
     * @return TcInf
     */
    public TcInf selectTeacher(String user_id){
        /**
         * q1：按照user_id,查询教师信息
         * q2：按照college_id，查询二级学院名称
         */
        TcInf tcInf = new TcInf();
        QueryWrapper<PtyTeacher> q1 = new QueryWrapper<>();
        q1.in("user_id",user_id);
        PtyTeacher ptyTeacher = ptyTeacherMapper.selectOne(q1);
        System.out.println(ptyTeacher);
        try {
            /**
             * 将对应的数据放入tcInf中输出
             */
//            tcInf.setCollege_name(ptyCollegeMapper.selectOne(q2).getCollegeName());
            tcInf.setCollege_name("1");
            tcInf.setId(ptyTeacher.getId());
            tcInf.setOpen_id(ptyTeacher.getOpenId());
            tcInf.setPhone(ptyTeacher.getPhone());
            tcInf.setUser_id(ptyTeacher.getUserId());
            return tcInf;
        }catch (Exception e){
            return null;
        }
    }
    @Autowired
    private Group group;
    /**
     * 删除学生用户基本信息
     */
    public boolean delPtyStu(String id){
        /**
         * queryWrapper：按照学生id，删除学生信息
         */
        try {
            group.delPtyStuGroupRelInStu(id);
            QueryWrapper<PtyStu> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id", id);
            ptyStuMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 删除学生用户基本信息
     */
    public boolean delPtyStuUser(String id){
        /**
         * queryWrapper：按照userid，删除学生信息
         */
        try {
            QueryWrapper<PtyStu> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("user_id", id);
            group.delPtyStuGroupRelInStu(ptyStuMapper.selectOne(queryWrapper).getId());
            ptyStuMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 删除教师用户基本信息
     */
    public boolean delPtyTeacher(String id){
        /**
         * queryWrapper：按照教师id，删除教师信息
         */
//        try {
            group.delPtyTeacherGroupRelInTeacher(id);
            group.delPtyStuGroupInTeacher(id);
//            notice.delPtyNoticeInTeacher(id);
            QueryWrapper<PtyTeacher> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",id);
            ptyTeacherMapper.delete(queryWrapper);
//        }catch (Exception e){
//            return false;
//        }
        return true;
    }
    @Autowired
    private Notice notice;
    /**
     * 删除教师用户基本信息
     */
    public boolean delPtyTeacherUser(String id){
        /**
         * queryWrapper：按照用户id，删除教师信息
         */
//        try {
            QueryWrapper<PtyTeacher> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("user_id",id);
            String teacher_id=ptyTeacherMapper.selectOne(queryWrapper).getId();
            group.delPtyTeacherGroupRelInTeacher(teacher_id);
            group.delPtyStuGroupInTeacher(teacher_id);
            notice.delPtyNoticeInTeacher(teacher_id);
            ptyTeacherMapper.delete(queryWrapper);
//        }catch (Exception e){
//            return false;
//        }
        return true;
    }


    /**
     * 更新学生用户的班级信息
     */
    public boolean updatePtyStuClass(String id,String class_id){
        try {
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("id",id);
            updateWrapper.set("class_id",class_id);
            ptyStuMapper.update(null,updateWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 更新学生用户学院信息
     */
    public boolean updatePtyStuCollege(String id,String college_id){
        try {
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("id",id);
            updateWrapper.set("college_id",college_id);
            ptyStuMapper.update(null,updateWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    /**
     * 更新教师用户学院信息
     */
    public boolean updatePtyTeacherCollege(String id,String college_id){
        try {
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("id",id);
            updateWrapper.set("college_id",college_id);
            ptyTeacherMapper.update(null,updateWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    /**
     * 查询学生数
     */
    public long countPtyStu(){
        return ptyStuMapper.selectCount(null);
    }
    /**
     * 查询教师数
     */
    public long countPtyTeacher(){
        return ptyTeacherMapper.selectCount(null);
    }
    /**
     * 查询所有学生
     */
    public List<PtyStu> selectAllPtyStu(){
        return ptyStuMapper.selectList(null);
    }
    /**
     * 查询所有教师
     */
    public List<PtyTeacher> selectAllPtyTeacher(){
        return ptyTeacherMapper.selectList(null);
    }

    /**
     * ======================================================================================================================================================================
     * ======================================================================================================================================================================
     * 已整理
     * ======================================================================================================================================================================
     * ======================================================================================================================================================================
     */

    /**
     * ===================================================================================================
     * 基础查询
     * ===================================================================================================
     */

    /**
     * 查询教师信息
     */
    public List<PtyTeacher> selectPtyTeacher(String id,String sex,String open_id,String phone,String user_id){
        QueryWrapper<PtyTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(sex),"sex",sex);
        queryWrapper.in(!StringUtils.isEmpty(open_id),"open_id",open_id);
        queryWrapper.in(!StringUtils.isEmpty(phone),"phone",phone);
        queryWrapper.in(!StringUtils.isEmpty(user_id),"user_id",user_id);
        return ptyTeacherMapper.selectList(queryWrapper);
    }
    /**
     * 查询教师信息数量
     */
    public long countPtyTeacher(String id,String sex,String open_id,String phone,String user_id){
        QueryWrapper<PtyTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(sex),"sex",sex);
        queryWrapper.in(!StringUtils.isEmpty(open_id),"open_id",open_id);
        queryWrapper.in(!StringUtils.isEmpty(phone),"phone",phone);
        queryWrapper.in(!StringUtils.isEmpty(user_id),"user_id",user_id);
        return ptyTeacherMapper.selectCount(queryWrapper);
    }
    /**
     * 查询教师信息
     * 分页
     */
    public List<PtyTeacher> selectPtyTeacherPage(String id,String sex,String open_id,String phone,String user_id,long current,long size){
        QueryWrapper<PtyTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(sex),"sex",sex);
        queryWrapper.in(!StringUtils.isEmpty(open_id),"open_id",open_id);
        queryWrapper.in(!StringUtils.isEmpty(phone),"phone",phone);
        queryWrapper.in(!StringUtils.isEmpty(user_id),"user_id",user_id);
        return ptyTeacherMapper.selectPage(new Page<PtyTeacher>(current,size),queryWrapper).getRecords();
    }
    /**
     * 查询教师信息
     * 列表查询
     */
    public List<PtyTeacher> selectPtyTeacherList(List<String> id,List<String> sex,List<String> open_id,List<String> phone,List<String> user_id){
        QueryWrapper<PtyTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(sex!=null,"sex",sex);
        queryWrapper.in(open_id!=null,"open_id",open_id);
        queryWrapper.in(phone!=null,"phone",phone);
        queryWrapper.in(user_id!=null,"user_id",user_id);
        return ptyTeacherMapper.selectList(queryWrapper);
    }
    /**
     * 查询教师信息数量
     * 列表查询
     */
    public long countPtyTeacherList(List<String> id,List<String> sex,List<String> open_id,List<String> phone,List<String> user_id){
        QueryWrapper<PtyTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(sex!=null,"sex",sex);
        queryWrapper.in(open_id!=null,"open_id",open_id);
        queryWrapper.in(phone!=null,"phone",phone);
        queryWrapper.in(user_id!=null,"user_id",user_id);
        return ptyTeacherMapper.selectCount(queryWrapper);
    }
    /**
     * 查询教师信息
     * 列表查询
     * 分页
     */
    public List<PtyTeacher> selectPtyTeacherListPage(List<String> id,List<String> sex,List<String> open_id,List<String> phone,List<String> user_id,long current,long size){
        QueryWrapper<PtyTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(sex!=null,"sex",sex);
        queryWrapper.in(open_id!=null,"open_id",open_id);
        queryWrapper.in(phone!=null,"phone",phone);
        queryWrapper.in(user_id!=null,"user_id",user_id);
        return ptyTeacherMapper.selectPage(new Page<PtyTeacher>(current,size),queryWrapper).getRecords();
    }
    /**
     * 查询学生信息
     */
    public List<PtyStu> selectPtyStu(String id,String sex,String open_id,String phone,String user_id){
        QueryWrapper<PtyStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(sex),"sex",sex);
        queryWrapper.in(!StringUtils.isEmpty(open_id),"open_id",open_id);
        queryWrapper.in(!StringUtils.isEmpty(phone),"phone",phone);
        queryWrapper.in(!StringUtils.isEmpty(user_id),"user_id",user_id);
        return ptyStuMapper.selectList(queryWrapper);
    }
    /**
     * 查询学生信息数量
     */
    public long countPtyStu(String id,String sex,String open_id,String phone,String user_id){
        QueryWrapper<PtyStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(sex),"sex",sex);
        queryWrapper.in(!StringUtils.isEmpty(open_id),"open_id",open_id);
        queryWrapper.in(!StringUtils.isEmpty(phone),"phone",phone);
        queryWrapper.in(!StringUtils.isEmpty(user_id),"user_id",user_id);
        return ptyStuMapper.selectCount(queryWrapper);
    }
    /**
     * 查询学生信息
     * 分页
     */
    public List<PtyStu> selectPtyStuPage(String id,String sex,String open_id,String phone,String user_id,long current,long size){
        QueryWrapper<PtyStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(sex),"sex",sex);
        queryWrapper.in(!StringUtils.isEmpty(open_id),"open_id",open_id);
        queryWrapper.in(!StringUtils.isEmpty(phone),"phone",phone);
        queryWrapper.in(!StringUtils.isEmpty(user_id),"user_id",user_id);
        return ptyStuMapper.selectPage(new Page<PtyStu>(current,size),queryWrapper).getRecords();
    }
    /**
     * 查询学生信息
     * 列表查询
     */
    public List<PtyStu> selectPtyStuList(List<String> id,List<String> sex,List<String> open_id,List<String> phone,List<String> user_id){
        QueryWrapper<PtyStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(sex!=null,"sex",sex);
        queryWrapper.in(open_id!=null,"open_id",open_id);
        queryWrapper.in(phone!=null,"phone",phone);
        queryWrapper.in(user_id!=null,"user_id",user_id);
        return ptyStuMapper.selectList(queryWrapper);
    }
    /**
     * 查询学生信息数量
     * 列表查询
     */
    public long countPtyStuList(List<String> id,List<String> sex,List<String> open_id,List<String> phone,List<String> user_id){
        QueryWrapper<PtyStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(sex!=null,"sex",sex);
        queryWrapper.in(open_id!=null,"open_id",open_id);
        queryWrapper.in(phone!=null,"phone",phone);
        queryWrapper.in(user_id!=null,"user_id",user_id);
        return ptyStuMapper.selectCount(queryWrapper);
    }
    /**
     * 查询学生信息
     * 列表查询
     * 分页
     */
    public List<PtyStu> selectPtyStuListPage(List<String> id,List<String> sex,List<String> open_id,List<String> phone,List<String> user_id,long current,long size){
        QueryWrapper<PtyStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(sex!=null,"sex",sex);
        queryWrapper.in(open_id!=null,"open_id",open_id);
        queryWrapper.in(phone!=null,"phone",phone);
        queryWrapper.in(user_id!=null,"user_id",user_id);
        return ptyStuMapper.selectPage(new Page<PtyStu>(current,size),queryWrapper).getRecords();
    }

    /**
     * ===================================================================================================
     * 基础删除
     * ===================================================================================================
     */

    /**
     * 删除教师信息
     */
    public boolean delPtyTeacher(String id,String sex,String open_id,String phone,String user_id){
        QueryWrapper<PtyTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(sex),"sex",sex);
        queryWrapper.in(!StringUtils.isEmpty(open_id),"open_id",open_id);
        queryWrapper.in(!StringUtils.isEmpty(phone),"phone",phone);
        queryWrapper.in(!StringUtils.isEmpty(user_id),"user_id",user_id);
        ptyTeacherMapper.delete(queryWrapper);
        return true;
    }
    /**
     * 删除教师信息
     * 列表删除
     */
    public boolean delPtyTeacherList(List<String> id,List<String> sex,List<String> open_id,List<String> phone,List<String> user_id){
        QueryWrapper<PtyTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(sex!=null,"sex",sex);
        queryWrapper.in(open_id!=null,"open_id",open_id);
        queryWrapper.in(phone!=null,"phone",phone);
        queryWrapper.in(user_id!=null,"user_id",user_id);
        ptyTeacherMapper.delete(queryWrapper);
        return true;
    }
    /**
     * 删除学生信息
     */
    public boolean delPtyStu(String id,String sex,String open_id,String phone,String user_id){
        QueryWrapper<PtyStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(sex),"sex",sex);
        queryWrapper.in(!StringUtils.isEmpty(open_id),"open_id",open_id);
        queryWrapper.in(!StringUtils.isEmpty(phone),"phone",phone);
        queryWrapper.in(!StringUtils.isEmpty(user_id),"user_id",user_id);
        ptyStuMapper.delete(queryWrapper);
        return true;
    }
    /**
     * 删除学生信息
     * 列表删除
     */
    public boolean delPtyStuList(List<String> id,List<String> sex,List<String> open_id,List<String> phone,List<String> user_id){
        QueryWrapper<PtyStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(sex!=null,"sex",sex);
        queryWrapper.in(open_id!=null,"open_id",open_id);
        queryWrapper.in(phone!=null,"phone",phone);
        queryWrapper.in(user_id!=null,"user_id",user_id);
        ptyStuMapper.delete(queryWrapper);
        return true;
    }

    /**
     * ===================================================================================================
     * 基础更新
     * ===================================================================================================
     */

    /**
     * 更新教师用户基本信息
     */
    public boolean updatePtyTeacher(PtyTeacher ptyTeacher){
//        try {
            QueryWrapper<PtyTeacher> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",ptyTeacher.getId());
            ptyTeacherMapper.update(ptyTeacher,queryWrapper);
//        }catch (Exception e){
//            return false;
//        }
        return true;
    }
    /**
     * 更新学生用户基本信息
     */
    public boolean updatePtyStu(PtyStu ptyStu){
//        try {
            QueryWrapper<PtyStu> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",ptyStu.getId());
            ptyStuMapper.update(ptyStu,queryWrapper);
//        }catch (Exception e){
//            return false;
//        }
        return true;
    }

    /**
     * ===================================================================================================
     * 基础新增
     * ===================================================================================================
     */

    /**
     * 添加教师用户基本信息
     * 需要随便向pojo中输入一个id
     */
    public String insertPtyTeacher(PtyTeacher ptyTeacher){
//        try {
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        ptyTeacher.setId(String.valueOf(snowflakeIdWorker.nextId()));
        ptyTeacherMapper.insert(ptyTeacher);
//        }catch (Exception e){
//            return ptyTeacher.getId();
//        }
        return ptyTeacher.getId();
    }
    /**
     * 添加学生用户基本信息
     * 需要随便向pojo中输入一个id
     */
    public String insertPtyStu(PtyStu ptyStu){
//        try {
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        ptyStu.setId(String.valueOf(snowflakeIdWorker.nextId()));
        ptyStuMapper.insert(ptyStu);
//        }catch (Exception e){
//            return ptyStu.getId();
//        }
        return ptyStu.getId();
    }
    /**
     * 批量
     * 添加教师用户基本信息
     * 需要随便向pojo中输入一个id
     */
    public int insertPtyTeacherList(List<PtyTeacher> ptyTeachers){
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        for (int i=0;i<ptyTeachers.size();i++){
            ptyTeachers.get(i).setId(String.valueOf(snowflakeIdWorker.nextId()));
        }
        return ptyTeacherMapper.insertBatchSomeColumn(ptyTeachers);
    }
    /**
     * 批量
     * 添加学生用户基本信息
     * 需要随便向pojo中输入一个id
     */
    public int insertPtyStuList(List<PtyStu> ptyStus){
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        for (int i=0;i<ptyStus.size();i++){
            ptyStus.get(i).setId(String.valueOf(snowflakeIdWorker.nextId()));
        }
        return ptyStuMapper.insertBatchSomeColumn(ptyStus);
    }
    /**
     * ===================================================================================================
     * 级联删除
     * ===================================================================================================
     */

    /**
     * ===================================================================================================
     * 复杂查询
     * ===================================================================================================
     */

}
